package cn.com.fssg.platform.base.handler.impl.gsl.zw;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 工商联执委信息删除数据接口
 */
@Component
public class GslZwDelCtl implements InterfaceHandler {

    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String zwId = jsonObject.getString("zw_id");
        String jcId = jsonObject.getString("jc_id");
        //获取gsl_jc_zw信息
        String sql = "SELECT * FROM gsl_jc_zw WHERE zw_id=:zw_id AND jc_id=:jc_id";
        Map<String, Object> param = new HashMap<>();
        param.put("zw_id", zwId);
        param.put("jc_id", jcId);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, param);
        if(!list.isEmpty()){
            Map<String, Object> gslJcZw1 = list.get(0);
            gslJcZw1.put("update_by", BizUtil.cur().curUserId());
            gslJcZw1.put("update_date", new Date());
            gslJcZw1.put("del_flag", "1");
            int count = updateDatabase(gslJcZw1, "gsl_jc_zw");
            if(count > 0) {
                //获取wx_user数据
                String queryWxUserSql = "select * from wx_user where id = :id";
                Map<String, Object> wxUserParam = new HashMap<>();
                wxUserParam.put("id", zwId);
                List<Map<String, Object>> wxUserList = jdbcTemplate.queryForList(queryWxUserSql, wxUserParam);
                if(!wxUserList.isEmpty()){
                    Map<String, Object> wxUser = wxUserList.get(0);
                    wxUser.put("update_by", BizUtil.cur().curUserId());
                    wxUser.put("update_date", new Date());
                    wxUser.put("del_flag", "1");
                    wxUser.put("login_name", wxUser.get("login_name") + BizUtil.cur().getSnowflakeIdStr());
                    updateDatabase(wxUser, "wx_user");
                }
                String queryWxDlUserSql = "select * from wx_user where parent_id = :id";
                Map<String, Object> wxDlUserParam = new HashMap<>();
                wxDlUserParam.put("id", zwId);
                List<Map<String, Object>> wxDlUserList = jdbcTemplate.queryForList(queryWxDlUserSql, wxDlUserParam);
                if(!wxDlUserList.isEmpty()){
                    Map<String, Object> dl = wxDlUserList.get(0);
                    dl.put("update_by", BizUtil.cur().curUserId());
                    dl.put("update_date", new Date());
                    dl.put("del_flag", "1");
                    dl.put("login_name", dl.get("login_name") + BizUtil.cur().getSnowflakeIdStr());
                    updateDatabase(dl, "wx_user");
                }

                //更新届次执委人数
                String jcUpdateSql = "select id from gsl_jc_zw_user where gsl_session = :jcId";
                Map<String, Object> jcUpdateParam = new HashMap<>();
                jcUpdateParam.put("jcId", jcId);
                List<Map<String, Object> > jcUserList = jdbcTemplate.queryForList(jcUpdateSql, jcUpdateParam);
                if(!jcUserList.isEmpty()) {
                    String updateSql = "UPDATE gsl_jc SET zw_count = :zwCount WHERE id = :jcId";
                    jcUpdateParam.put("zwCount", jcUserList.size());
                    jdbcTemplate.update(updateSql, jcUpdateParam);
                }
            }
            return count;
        }
        return null;
    }

    /** 更新数据库数据 */
    public int updateDatabase(Map<String, Object> param, String tableName) {
        List<String> fields = new ArrayList<>();
        param.forEach((k, v) -> {
            if (v != null && !"id".equals(k)) {
                fields.add(k + " = :" + k);
            }
        });

        String sql = String.format("UPDATE %s SET %s WHERE id = '%s'",
                tableName,
                String.join(", ", fields),
                param.get("id")
        );
        return jdbcTemplate.update(sql, param);
    }
}